import dt from '@dt/dt'
import orderApi from '@/config/server/api/order.js'
import userApi from '@/config/server/api/user.js'
import buildingApi from '@/config/server/api/building.js'

// 搜索获取人员

let workList = []

function getOaPeople(workNo) {
  if (!workNo.replace(/\s+/g, '').length) {
    return new Promise((resovle, reject) => {
      resovle()
    }).then((res) => {
      workList = []
      return workList
    })
  } else {
    // if (workNo.length < 5 || workNo.length > 8) return Promise.resolve();
    return userApi.teams.getByWorkNoOrName
      .fetch({
        searchKey: workNo,
        validate: 3
      })
      .then((res) => {
        workList = res.map((item) => {
          item.key = item.workNo
          item.value = item.userName + '（' + item.workNo + '）'
          return item
        })
        return workList
      })
  }
}

function createPeople(opt = {}) {
  return {
    title: '人员',
    key: opt.key || 'workNo',
    placeholder: '请输入人员姓名或工号',
    select: {
      clearable: true,
      filterable: true,
      options: workList.length > 0 ? workList : [],
      remote: (key) => {
        return getOaPeople(key)
      }
    },
    // change: opt.change,
    data: {
      from: (info) => {
        // if (info.workNo && info.userName) {
        //   return info.workNo + '-' + info.userName
        // }
        // return null
      },
      to: (v) => {
        let info = {
          workNo: null
        }
        if (v) {
          console.log(v, 'vvvvvvvvvvvvvv')
          info.workNo = v.value
        }
        if (opt.to) {
          return opt.to(info)
        }
        return info
      }
    },
    rules: [
      {
        required: true,
        message: '请输入姓名或工号',
        trigger: 'blur'
      }
    ]
  }
}

function selectBonusStimulateList(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.incentive.selectBonusStimulateList.fetch(info)
}

function saveBonusStimulate(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.incentive.saveBonusStimulate.fetch(info)
}

function selectBonusStimulateInfo(info) {
  return orderApi.bonus.incentive.selectBonusStimulateInfo.fetch(info)
}

function saveBonusStimulateDetail(info) {
  return orderApi.bonus.incentive.saveBonusStimulateDetail.fetch(info)
}

function selectBonusStimulateRepeat(info) {
  return orderApi.bonus.incentive.selectBonusStimulateRepeat.fetch(info)
}

function submitBonusStimulate(info) {
  return orderApi.bonus.incentive.submitBonusStimulate.fetch(info)
}

function updateBonusStimulateDetail(info) {
  return orderApi.bonus.incentive.updateBonusStimulateDetail.fetch(info)
}

function delBonusStimulateDetail(info) {
  return orderApi.bonus.incentive.delBonusStimulateDetail.fetch(info)
}

function roomList(info) {
  return buildingApi.room.roomList.fetch(info)
}

function emptyBonusStimulateDetail(info) {
  return orderApi.bonus.incentive.emptyBonusStimulateDetail.fetch(info)
}
//在销激励模板
function exportBonusStimulateDetail(info) {
  return orderApi.bonus.incentive.exportBonusStimulateDetail.fetch(info)
}

function importBonusStimulateDetail(commissionId) {
  let upload = orderApi.bonus.incentive.importBonusStimulateDetail
  upload.setData(commissionId)
  return upload
}

function exportBonusStimulate(info) {
  return orderApi.bonus.incentive.exportBonusStimulate.fetch(info)
}

function delBonusStimulate(info) {
  return orderApi.bonus.incentive.delBonusStimulate.fetch(info)
}

function copyBonusStimulate(info) {
  return orderApi.bonus.incentive.copyBonusStimulate.fetch(info)
}

// 项目津贴

function saveBonusAllowance(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.allowance.saveBonusAllowance.fetch(info)
}

function selectBonusAllowanceRepeat(info) {
  return orderApi.bonus.allowance.selectBonusAllowanceRepeat.fetch(info)
}

function selectBonusAllowanceList(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.allowance.selectBonusAllowanceList.fetch(info)
}

function selectBonusAllowanceInfo(info) {
  return orderApi.bonus.allowance.selectBonusAllowanceInfo.fetch(info)
}

function saveBonusAllowanceDetail(info) {
  return orderApi.bonus.allowance.saveBonusAllowanceDetail.fetch(info)
}

function submitBonusAllowance(info) {
  return orderApi.bonus.allowance.submitBonusAllowance.fetch(info)
}

function delBonusAllowance(info) {
  return orderApi.bonus.allowance.delBonusAllowance.fetch(info)
}

function exportBonusAllowance(info) {
  return orderApi.bonus.allowance.exportBonusAllowance.fetch(info)
}

function copyBonusAllowance(info) {
  return orderApi.bonus.allowance.copyBonusAllowance.fetch(info)
}

// function importBonusAllowanceDetail(info) {
//   return orderApi.bonus.allowance.importBonusAllowanceDetail.fetch(info)
// }
function importBonusAllowanceDetail(commissionId) {
  let upload = orderApi.bonus.allowance.importBonusAllowanceDetail
  upload.setData(commissionId)
  return upload
}

function emptyBonusAllowanceDetail(info) {
  return orderApi.bonus.allowance.emptyBonusAllowanceDetail.fetch(info)
}

function delBonusAllowanceDetail(info) {
  return orderApi.bonus.allowance.delBonusAllowanceDetail.fetch(info)
}
//项目津贴模板
function exportBonusAllowanceDetail(info) {
  return orderApi.bonus.allowance.exportBonusAllowanceDetail.fetch(info)
}

export default {
  selectBonusStimulateList,
  saveBonusStimulate,
  selectBonusStimulateInfo,
  createPeople,
  roomList,
  saveBonusStimulateDetail,
  selectBonusStimulateRepeat,
  submitBonusStimulate,
  updateBonusStimulateDetail,
  delBonusStimulateDetail,
  emptyBonusStimulateDetail,
  exportBonusStimulateDetail,
  importBonusStimulateDetail,
  exportBonusStimulate,
  delBonusStimulate,
  copyBonusStimulate,
  // 项目津贴
  saveBonusAllowance,
  selectBonusAllowanceRepeat,
  selectBonusAllowanceList,
  selectBonusAllowanceInfo,
  saveBonusAllowanceDetail,
  submitBonusAllowance,
  delBonusAllowance,
  exportBonusAllowance,
  copyBonusAllowance,
  importBonusAllowanceDetail,
  emptyBonusAllowanceDetail,
  delBonusAllowanceDetail,
  exportBonusAllowanceDetail
}
